<?php
/**
 * Created by PhpStorm.
 * User: 老毛
 * Date: 2019/9/4
 * Time: 18:44
 */

namespace app\api\controller;
use think\Db;
use think\Validate;

class DataTokenTow extends TokenBase
{
    //系统统一删除 真删除
    public function deldata(){
        $get = $_GET;
        $tableName = "php_tongzhi";
        $map = [];
        if(!$get['name']) return ajaxReturn('删除失败！',99);
        //删除系统通知
        if($get['name'] == 'tongzhi'){
            $tableName = "php_tongzhi"; $map = ['id' => $get['uid']];
        }
        //删除个人圈子
        if($get['name'] == 'quanzhi'){
            $tableName = "php_quanzhi"; $map = ['id' => $get['uid']];
        }
        $datadel = Db::name($tableName)->where($map)->delete();
        if(!$datadel) return ajaxReturn('删除失败！',99);
        return ajaxReturn('删除成功！',1);
    }

    //获取自己圈子圈子详情
    public function getmySelfquan(){
        $get = $_GET;
        if(!isset($get['page'])) return ajaxReturn('获取失败！',99);

        $data = Db::name('php_quanzhi')
            ->alias('a')->join('php_member b','a.uid = b.id')
            ->field('a.*,b.nickname,b.image as userimage')
            ->where(['a.status' => 0,'a.uid' => $this->uid])->order('a.id desc')->page($get['page'],10)->select();
        if(!$data) return ajaxReturn('暂无消息',99);
        //将查询出来的数据里面的图片 改为数组
        foreach ($data as $k => $v){
           if($v['image']){
               $data[$k]['imageall'] = explode('|',$v['image']);
               foreach ($data[$k]['imageall'] as $key=>$value){
                   $data[$k]['imageall'][$key] = WEB_URL.'/'.$value;
               }
           }
            $data[$k]['userimage'] = WEB_URL.'/'. $v['userimage'];
        }
        if(!is_array($data)) return ajaxReturn('错误',99);
        if(count($data) > 0){
            return ajaxReturn('获取成功',1,$data);
        }else{
            return ajaxReturn('这个已经是最后一页啦',2,$data);
        }
    }

    //成为商家验证
    public function activeStore(){
        $post = $_POST;
        $rule = [
            'store_name'            => 'require',
            'store_cate'            => 'require',
            'store_f_name'            => 'require',
            'store_mobile'            => 'require|[1][3-9][0-9]{9}',
            'store_card_image'            => 'require',
            'store_address'            => 'require',
            'store_images'            => 'require',
        ];

        $msg = [
            'store_name.require'           => '商家名称不能为空',
            'store_cate.require'           => '请选择商家分类',
            'store_f_name.require'           => '请选择商家所属法人姓名',
            'store_mobile.require'           => '请输入商家联系电话',
            'store_mobile'           => '请输入正确的联系电话',
            'store_card_image.require'           => '请上传身份证正反面',
            'store_address.require'           => '请输入商家地址',
            'store_images.require'           => '请上传商家环境图片',

        ];
        $validate = new Validate($rule, $msg);
        if(!$validate->check($post))
        {
            return ajaxReturn($validate->getError(),99);
        }
        //判断是否存在座机电话
        if(!$post['store_phone'])  unset($post['store_phone']);
        $post['store_card_image'] = str_replace(',','|',$post['store_card_image']);
        $post['store_images'] = str_replace(',','|',$post['store_images']);
        $post['store_uid'] = $this->uid;

        //根据商家地址 获取地址所在经纬度

        //验证完成
        $data = Db::name('php_min')->insert($post);
        if(!$data) return ajaxReturn('提交失败！',99);
        return ajaxReturn('提交成功',1);
    }
}